package cliente.vista;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;

import cliente.controle.ClienteControlador;
import cliente.controle.GerenteControlador;
import cliente.controle.Sistema;

public class LoginView {

	private JFrame tela;
	private JPanel painel;
	private JLabel loginLabel;
	private JLabel senhaLabel;
	private JTextField login;
	private JPasswordField senha;
	private JButton botao;
	private ActionListener ouvinte;

	public void telaLogin() {
		Color color = new Color(179, 217, 236);
		ouvinte = new acao();
		tela = new JFrame();
		tela.setBounds(500, 300, 300, 150);
		tela.setResizable(false);
		tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// coloca o titulo certo
		Sistema sistema = Sistema.getUnicaInstancia();
		tela.setTitle("Login " + (sistema.isCliente() ? "Cliente" : "Gerente"));

		painel = new JPanel();

		GroupLayout layout = new GroupLayout(painel);
		painel.setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		loginLabel = new JLabel("Login:");
		senhaLabel = new JLabel("Senha:");
		login = new JTextField();
		senha = new JPasswordField();
		senha.addActionListener(ouvinte);
		botao = new JButton("Conectar");
		botao.addActionListener(ouvinte);

		organizaFields(layout);

		painel.setBackground(color);
		tela.setLayout(new BorderLayout());
		tela.add(painel, BorderLayout.CENTER);
		tela.add(botao, BorderLayout.SOUTH);
		tela.setVisible(true);
	}

	private void criaGruposVerticais(GroupLayout layout) {
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(loginLabel).addComponent(login));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(senhaLabel).addComponent(senha));
		layout.setVerticalGroup(vGroup);
	}

	private void criaGruposHorizontais(GroupLayout layout) {
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup().addComponent(loginLabel)
				.addComponent(senhaLabel));
		hGroup.addGroup(layout.createParallelGroup().addComponent(login)
				.addComponent(senha));
		layout.setHorizontalGroup(hGroup);
	}

	private void organizaFields(GroupLayout layout) {
		criaGruposHorizontais(layout);
		criaGruposVerticais(layout);
	}

	public JFrame getTela() {
		return tela;
	}

	public JTextField getLogin() {
		return login;
	}

	public JPasswordField getSenha() {
		return senha;
	}

	// Essa classe só passará os dados para a classe de controle adequada
	class acao implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Sistema s = Sistema.getUnicaInstancia();
			if (s.isCliente()) {
				ClienteControlador.logar(LoginView.this, login.getText(),
						String.copyValueOf(senha.getPassword()));
			} else {
				GerenteControlador.logar(LoginView.this, login.getText(),
						String.copyValueOf(senha.getPassword()));
			}
		}
	}

	public void zeraTelaLogin() {
		login.setText("");
		senha.setText("");
	}

}
